package com.example.ssmlab2.controller;

import com.example.ssmlab2.model.Book;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.IOException;

/**
 * ClassName:BookController
 * Package:com.example.ssmlab2.controller
 * Description:
 *
 * @Author:ZhangXin
 * @Create:2023/4/1-19:42
 * @Version:v1.0
 */
@Controller
@RequestMapping("book")
public class BookController extends BaseController {
    private SqlSession sqlSession;

    public BookController(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
    }

    @RequestMapping(value = "queryById/{id}", method = RequestMethod.GET)
    private String queryById(@PathVariable("id") int id) {
        System.out.println(id);
        Book book = sqlSession.selectOne("book.queryById", id);
        session.setAttribute("book", book);
        return "redirect:/admin/edit.jsp";
    }

    @RequestMapping(value = "modify", method = RequestMethod.POST)
    private String modify(Book book) {
        sqlSession.update("book.modify", book);
        session.setAttribute("books", sqlSession.selectList("book.queryAllBooks"));
        return "redirect:/admin/bookManagement.jsp";
    }

    //图书删除
    @RequestMapping(value = "removeById/{id}",method = RequestMethod.GET)
    private String removeById(@PathVariable("id") int id){
        sqlSession.delete("book.removeById",id);
        session.setAttribute("book",sqlSession.selectList("book.queryAllBooks"));
        return "redirect:/admin/bookManagement.jsp";
    }


    //图书的详细信息
    @RequestMapping(value = "detail/{id}",method = RequestMethod.GET)
    private String detail(@PathVariable("id") int id){
        System.out.println(id);
        Book book = sqlSession.selectOne("book.queryById",id);
        session.setAttribute("books",book);
        return "redirect:/admin/detail.jsp";
    }

    //退出
    @RequestMapping(value = "signOut2",method = RequestMethod.GET)
    private String signOut2(){
        session.invalidate();
        return "forward:/admin/bookManagement.jsp";
    }

}
